package com.ynding.spring.idea.ddd.core.model.lottery.valobj;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author ynding
 * @version 2022/9/3
 **/
@Data
@ApiModel(value = "AwardPool", description = "奖池")
public class AwardPool {

    @ApiModelProperty("奖池支持的城市")
    private String cityIds;

    @ApiModelProperty("奖池支持的得分")
    private String scores;

    @ApiModelProperty("奖池匹配的用户类型")
    private int userGroupType;

    @ApiModelProperty("奖池中包含的奖品")
    private List<Award> awards;

    /**
     * 当前奖池是否与城市匹配
     *
     * @param cityId
     * @return
     */
    public boolean matchedCity(int cityId) {
        // TODO
        return false;
    }

    /**
     * 当前奖池是否与用户得分匹配
     *
     * @param score
     * @return
     */
    public boolean matchedScore(int score) {
        // TODO
        return false;
    }

    /**
     * 根据概率选择奖池
     *
     * @return
     */
    public Award randomGetAward() {
        int sumOfProbability = 0;
        for (Award award : awards) {
            sumOfProbability += award.getProbability();
        }
        int randomNumber = ThreadLocalRandom.current().nextInt(sumOfProbability);
        int range = 0;
        for (Award award : awards) {
            range += award.getProbability();
            if (randomNumber < range) {
                return award;
            }
        }
        return null;
    }

}
